package com.lwlk.util.string;

import static com.lwlk.util.functional.Common.returnThat;

// 协助处理多行字符串的工具
public class Lines {

    private String left = "", spliter = "", right = "";

    private String templater = "\\?";

    private String[] strings;

    public Lines(String ... strs) { this.strings = strs; }

    // 设置字符串之间的分隔符
    public Lines with(String spliter) { return returnThat(() -> this.spliter = spliter, this); }

    // 设置包围符和字符串之间的分隔符
    public Lines with(String left, String spliter, String right) {
        return returnThat(() -> { this.left = left; this.spliter = spliter; this.right = right; }, this);
    }

    // 设置模板占位符
    public Lines template(String templater) { return returnThat(() -> this.templater = templater, this); }

    // 生成融合了包围符和分隔符的字符串
    public String content() { return left + String.join(spliter, strings) + right; }

    // 使用N个参数替换模板占位符 无异常处理 需要悠着使用
    public String content(String ... args) {
        return left + zip(String.join(spliter, strings).split(templater), args) + right;
    }

    // 交替连接a和b数组中的字符串
    private String zip(String[] a, String[] b) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < a.length && i < b.length; ++i) {
            sb.append(a[i]).append(b[i]);
        }
        if(a.length != b.length && a.length - 1 == b.length) {
            sb.append(a[a.length - 1]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        String sql = new Lines("select id, name, age, msg from user where id = ? and name like ? and age between(?, ?)").content("100", "'Smi'", "18", "25");

        System.out.println(sql);
    }
}
